import axios from 'axios'

const api = axios.create({
    baseURL: 'http://localhost:8080/api',
    timeout: 5000
})

// 连接测试
export const ping = () => {
    return api.get('/redis/ping')
}

// 获取配置信息
export const getConfig = () => {
    return api.get('/redis/config')
}

// 获取当前环境配置
export const getCurrentConfig = () => {
    return api.get('/redis/current')
}

// Environment
export const switchEnvironment = (environment: string) => {
    return api.post('/redis/environment', { environment })
}

// String Operations
export const setString = (key: string, value: string, expire?: number) => {
    const data: any = { key, value }
    if (expire && expire > 0) {
        data.expire = expire.toString()
    }
    return api.post('/redis/string/set', data)
}

export const getString = (key: string) => {
    return api.get(`/redis/string/${key}`)
}

// 删除键
export const del = (key: string) => {
    return api.delete(`/redis/string/${key}`)
}

// 检查键是否存在
export const exists = (key: string) => {
    return api.get(`/redis/key/${key}/exists`)
}

// 获取键的TTL
export const ttl = (key: string) => {
    return api.get(`/redis/key/${key}/ttl`)
}

// 设置键的过期时间
export const expire = (key: string, seconds: number) => {
    return api.post(`/redis/key/${key}/expire`, { seconds })
}

// Hash Operations
export const hSet = (key: string, field: string, value: string) => {
    return api.post('/redis/hash/set', { key, field, value })
}

export const hGet = (key: string, field: string) => {
    return api.get(`/redis/hash/${key}/${field}`)
}

export const hGetAll = (key: string) => {
    return api.get(`/redis/hash/${key}`)
}

export const hDel = (key: string, field: string) => {
    return api.delete(`/redis/hash/${key}/${field}`)
}

// List Operations
export const lPush = (key: string, values: string[]) => {
    return api.post('/redis/list/lpush', { key, values })
}

export const rPush = (key: string, values: string[]) => {
    return api.post('/redis/list/rpush', { key, values })
}

export const lPop = (key: string) => {
    return api.post('/redis/list/lpop', { key })
}

export const rPop = (key: string) => {
    return api.post('/redis/list/rpop', { key })
}

export const lRange = (key: string, start: number, end: number) => {
    return api.get(`/redis/list/${key}`, { params: { start, end } })
}

// Set Operations
export const sAdd = (key: string, members: string[]) => {
    return api.post('/redis/set/add', { key, members })
}

export const sRem = (key: string, members: string[]) => {
    return api.post('/redis/set/remove', { key, members })
}

export const sMembers = (key: string) => {
    return api.get(`/redis/set/${key}`)
}

export const sIsMember = (key: string, member: string) => {
    return api.get(`/redis/set/${key}/member/${member}`)
}

// ZSet Operations
export const zAdd = (key: string, score: number, member: string) => {
    return api.post('/redis/zset', { key, score, member })
}

export const zRange = (key: string, start: number, end: number, withScores: boolean = false) => {
    return api.get(`/redis/zset/${key}`, { params: { start, end, withScores } })
}

export const zRevRange = (key: string, start: number, end: number, withScores: boolean = false) => {
    return api.get(`/redis/zset/rev/${key}`, { params: { start, end, withScores } })
}

export const zRangeByScore = (key: string, min: number, max: number, withScores: boolean = false) => {
    return api.get(`/redis/zset/score/${key}`, { params: { min, max, withScores } })
}

export const zRevRangeByScore = (key: string, max: number, min: number, withScores: boolean = false) => {
    return api.get(`/redis/zset/revscore/${key}`, { params: { min, max, withScores } })
}

export const zRem = (key: string, member: string) => {
    return api.delete(`/redis/zset/${key}/${member}`)
}

// Advanced Operations
export const executeTransaction = (commands: any[]) => {
    return api.post('/redis/transaction', { commands })
}

export const executePipeline = (commands: any[]) => {
    return api.post('/redis/pipeline', { commands })
}

export const executeScript = (script: string, keys: string[], args: string[]) => {
    return api.post('/redis/script', { script, keys, args })
}

// 获取所有键信息
export const getAllKeys = (pattern: string = '*') => {
    return api.get('/redis/keys', { params: { pattern } })
}

// 删除所有数据
export const deleteAllData = () => {
    return api.delete('/redis/all')
}